package com.ruoyi.service.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.service.utils.ServiceUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.service.domain.Surplus;
import com.ruoyi.service.service.ISurplusService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;

/**
 * 富余电量 信息操作处理
 * 
 * @author john
 * @date 2019-01-03
 */
@Controller
@RequestMapping("/service/surplus")
public class SurplusController extends BaseController
{
    private String prefix = "service/surplus";
	
	@Autowired
	private ISurplusService surplusService;
	@Autowired
	private ServiceUtils utils;
	
	@GetMapping()
	public String surplus()
	{
	    return prefix + "/surplus";
	}
	
	/**
	 * 查询富余电量列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Surplus surplus)
	{
		startPage();
        List<Surplus> list = surplusService.selectSurplusList(surplus);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出富余电量列表
	 */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Surplus surplus)
    {
    	List<Surplus> list = surplusService.selectSurplusList(surplus);
        ExcelUtil<Surplus> util = new ExcelUtil<Surplus>(Surplus.class);
        return util.exportExcel(list, "surplus");
    }
	
	/**
	 * 新增富余电量
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存富余电量
	 */
	@Log(title = "富余电量", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Surplus surplus)
	{		
		return toAjax(surplusService.insertSurplus(surplus));
	}

	/**
	 * 修改富余电量
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Surplus surplus = surplusService.selectSurplusById(id);
		mmap.put("month", utils.getMonth(surplus.getBasicsId()));
		mmap.put("surplus", surplus);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存富余电量
	 */
	@Log(title = "富余电量", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Surplus surplus)
	{
		surplus.setFTotal(surplus.getFF().add(surplus.getFP()).add(surplus.getFG()));
		surplus.setPTotal(surplus.getPF().add(surplus.getPP()).add(surplus.getPG()));
		surplus.setKTotal(surplus.getKF().add(surplus.getKP()).add(surplus.getKG()));
		surplus.setSurplusTotal(surplus.getFTotal().add(surplus.getPTotal()).add(surplus.getKTotal()));
		surplus.setUpAccount (surplus.getFactoryPrice().multiply(surplus.getSurplusTotal()));
		surplus.setSPAccount(surplus.getSPPrice().multiply(surplus.getSurplusTotal()));
		surplus.setJJAccount(surplus.getJJAverage().multiply(surplus.getSurplusTotal()));
//		surplus.setEleAccount(surplus.getDHAverage().multiply(surplus.getSurplusTotal()));

//		用户承担电费金额eleAccount = 电量上网电费upAccount + 输配收入金额sPAccount
		surplus.setEleAccount(surplus.getUpAccount().add(surplus.getSPAccount()));
//      到户均价dHAverage = 用户承担电费金额eleAcount / 交易电量合计tradeTotal
		BigDecimal TradeTotal = surplus.getSurplusTotal();
		if (TradeTotal == BigDecimal.valueOf(0)) {
			surplus.setDHAverage(BigDecimal.ZERO);
		}else{
			surplus.setDHAverage(surplus.getEleAccount().divide(TradeTotal).setScale(6,BigDecimal.ROUND_HALF_UP));
		}
		int i = surplusService.updateSurplus(surplus);
		utils.total(surplus.getPointId(),surplus.getBasicsId());
		return toAjax(i);
	}
	
	/**
	 * 删除富余电量
	 */
	@Log(title = "富余电量", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(surplusService.deleteSurplusByIds(ids));
	}
	
}
